package cn.codexin.wechatofficial.controller;

import cn.codexin.wechatofficial.mapper.HTDB.dbo.StationMapper;
import cn.codexin.wechatofficial.mapper.HTDB.dbo.UnapprovedUserMapper;
import cn.codexin.wechatofficial.mapper.HTDB.dbo.UserMapper;
import cn.codexin.wechatofficial.model.dbo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：zhanfan
 * @date ：Created in 2020/8/17 23:12
 */

@RestController
@RequestMapping(value = "admin", produces = "application/json;charset=UTF-8")
public class AdminController {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UnapprovedUserMapper unapprovedUserMapper;

    @Autowired
    StationMapper stationMapper;

    @GetMapping("userList")
    public Map<String, Object> userList() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<User> users = userMapper.selectByExample(new UserExample());
            map.put("state", true);
            map.put("msg", "查询成功");
            map.put("userList", users);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "查询失败,提示：" + e.getMessage());
        }
        return map;
    }

    @GetMapping("unapprovedList")
    public Map<String, Object> unapprovedList() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<UnapprovedUser> unapprovedUsers = unapprovedUserMapper.selectByExample(new UnapprovedUserExample());
            map.put("state", true);
            map.put("msg", "查询成功");
            map.put("unapprovedList", unapprovedUsers);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "查询失败,提示：" + e.getMessage());
        }
        return map;
    }

    @GetMapping("verifyByID")
    public Map<String, Object> approveByID(@RequestParam int ID, @RequestParam boolean approve) {
        Map<String, Object> map = new HashMap<>();
        try {
            if (approve) {
                UnapprovedUser unapprovedUser = unapprovedUserMapper.selectByPrimaryKey(ID);
                UserExample userExample = new UserExample();
                userExample.createCriteria().andOpenidEqualTo(unapprovedUser.getOpenid());
                List<User> users = userMapper.selectByExample(userExample);
                if(users.isEmpty()){
                    //构造user
                    User user = new User();
                    user.setName(unapprovedUser.getName());
                    user.setOpenid(unapprovedUser.getOpenid());
                    user.setPhoneNumber(unapprovedUser.getPhoneNumber());
                    user.setDefaultSite(unapprovedUser.getStation());
                    user.setGmtCreate(System.currentTimeMillis());
                    user.setGmtModified(user.getGmtCreate());
                    userMapper.insertSelective(user);
                }
                //构造station
                Station station = new Station();
                station.setOpenid(unapprovedUser.getOpenid());
                station.setStation(unapprovedUser.getStation());
                station.setGmtCreate(System.currentTimeMillis());
                station.setGmtModified(station.getGmtCreate());
                //执行数据库操作
                stationMapper.insertSelective(station);
            }
            unapprovedUserMapper.deleteByPrimaryKey(ID);
            map.put("state", true);
            map.put("msg", "操作成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "操作失败,提示：" + e.getMessage());
        }
        return map;
    }
    @GetMapping("deleteByID")
    public Map<String, Object> deleteByID(@RequestParam int ID) {
        Map<String, Object> map = new HashMap<>();
        try {
            User deleteUser = userMapper.selectByPrimaryKey(ID);
            StationExample stationExample = new StationExample();
            stationExample.createCriteria().andOpenidEqualTo(deleteUser.getOpenid());
            stationMapper.deleteByExample(stationExample);
            userMapper.deleteByPrimaryKey(ID);
            map.put("state", true);
            map.put("msg", "操作成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "操作失败,提示：" + e.getMessage());
        }
        return map;
    }
}
